package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"time"
)

type User struct {
	Id           int64     `orm:"pk;auto"`
	Name         string    `orm:"size(20)";db:"name"` //用户名称
	ThemeId      int64     `orm:"size(1)"`            //系统偏好设置
	ThemePath    string    `orm:"size(11)"`           //手机号
	Phone        string    `orm:"size(11)"`           //手机号
	Email        string    `orm:"size(50)"`           //邮箱
	Password     string    `orm:"size(50)"`           //密码
	DepartmentId int64     `orm:"size(11)"`           //部门
	Department   string    `orm:"size(255)"`
	Job          string    `orm:"size(50)"`  //职业，工作
	Address      string    `orm:"size(255)"` //地址
	Avatar       string    `orm:"size(255)"` //头像地址
	RoleId       int64     `orm:"size(11)"`  //权限编号
	CheckStatus  int64     `orm:"size(1)"`   //审核状态
	RegisterType string    `orm:"size(1)"`   //注册类型 0：pc;1:手机
	CreateAt     time.Time `orm:"auto_now_add;type(datetime)"form:"-"`
	CreateBy     int64     `orm:"size(11)"` //用户名称
	CreateName   string    `orm:"size(20)"` //用户名称
	UpdateAt     time.Time `orm:"auto_now;type(datetime)"`
	UpdateName   string    `orm:"size(20)"` //更新者姓名
	UpdateBy     int64     `orm:"size(11)"` //更新者姓名
	Timestamp    int64     `orm:"size(30)"`
}

// 获取模型名称
// 获取模型表名
func (m *User) TableName() string {
	return "user"
}

// 获取全部用户
func (m *User) GetAllUser() []*User {
	info := User{}
	list := make([]*User, 0)
	_, _ = info.Query().All(&list)
	return list
}

// 查询方法
func (m *User) Query() orm.QuerySeter {
	return orm.NewOrm().QueryTable(m)
}

// 添加用户
func (m *User) Insert() error {
	_, err := orm.NewOrm().Insert(m)
	return err
}

// 删除用户
func (m *User) Delete() error {
	_, err := orm.NewOrm().Delete(m)
	return err
}

// 修改用户信息
func (m *User) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(m, fields...)
	return err
}
func (m *User) Read(fields ...string) (int64, error) {
	if err := orm.NewOrm().Read(m, fields...); err != nil {
		return 0, err
	}
	return m.Id, nil
}

// 通过email或者单个用户信息
func (m *User) GetUserByEmail(email string) (*User, error) {
	info := &User{}
	err := info.Query().Filter("email", email).One(info)
	return info, err
}

func (m *User) LoginUserByEmail(email string) (*User, error) {
	info := &User{}
	err := info.Query().Filter("email", email).Filter("check_status", 1).One(info)
	return info, err
}

// 通过email或者单个用户信息
func (m *User) GetUserById(id int64) (*User, error) {
	err := m.Query().Filter("id", id).One(m)
	return m, err
}

// 获取用户总数
func (m *User) GetUserCounts() int64 {
	qs := orm.NewOrm().QueryTable(m)
	count, _ := qs.Filter("check_status", 1).Count()
	return count
}

// 获取每天的用户数
func (m *User) GetUserPerDayCounts(string2 string) int64 {
	strMin := string2 + " 00:00:00"
	strMax := string2 + " 23:59:59"
	fmt.Println("-----------------", strMin, strMax)
	qs := orm.NewOrm().QueryTable(m)
	cnt, _ := qs.Filter("check_status", 1).Filter("create_at__gte", strMin).Filter("create_at__lte", strMax).Count()
	return cnt
}

// 获取每天区间的用户数
func (m *User) GetUserRandDayCounts(old string, new string) int64 {
	strMin := old + " 00:00:00"
	strMax := new + " 23:59:59"
	fmt.Println("-----------------", strMin, strMax)
	qs := orm.NewOrm().QueryTable(m)
	cnt, _ := qs.Filter("check_status", 1).Filter("create_at__gte", strMin).Filter("create_at__lte", strMax).Count()
	return cnt
}

func (m *User) GetUserByIdAndEmail(id int64, email string) (*User, error) {
	err := m.Query().Filter("check_status", 1).Filter("id", id).Filter("email", email).One(m)
	return m, err
}
